package com.lin.factorial;

import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.compression.ZlibCodecFactory;
import io.netty.handler.codec.compression.ZlibWrapper;
import io.netty.handler.ssl.SslContext;

public class FactorialServerInitializer extends ChannelInitializer {
	private SslContext sslContext;

	public FactorialServerInitializer(SslContext sslContext) {
		this.sslContext = sslContext;
	}

	@Override
	protected void initChannel(Channel ch) {
		ChannelPipeline pipeline = ch.pipeline();
		if (sslContext != null){
			pipeline.addLast(sslContext.newHandler(ch.alloc()));
		}

		pipeline.addLast(ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP));
		pipeline.addLast(ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP));

		pipeline.addLast(new BigIntegerDecoder());
		pipeline.addLast(new NumberEncoder());

		pipeline.addLast(new FactorialServerHandler());
	}
}
